﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace ScoreManage
{
    public partial class User : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void BindGrid()
        {
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            string strSql = "SELECT * FROM ctview";
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataAdapter da = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "ctview");
            GridView1.DataSource = ds.Tables["ctview"];
            GridView1.DataBind();
        }

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            string parmString = ((LinkButton)sender).CommandArgument.ToString();
            string[] parmStr = parmString.Split(',');
            string Cno = parmStr[0];
            string Tno = parmStr[1];
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            conn.Open();
            string strSql1 = string.Format("SELECT * FROM student_course WHERE cno='{0}'", Cno);
            SqlCommand myCommand1 = new SqlCommand(strSql1, conn);
            SqlDataReader dr = myCommand1.ExecuteReader();
            if (dr.Read())
            {
                ClientScript.RegisterStartupScript(this.GetType(), "error", "<script>alert('该门课程仍有学生选课中，不能删除！')</script>");
            }
            else 
            {
                dr.Close();
                string strSql;
                if (Tno == "") {
                    strSql = string.Format("DELETE course WHERE cno = '{0}'", Cno);
                } else {
                    strSql = string.Format("DELETE teacher_course WHERE cno = '{0}' AND tno = '{1}'", Cno, Tno);
                }
                SqlCommand myCommand = new SqlCommand(strSql, conn);
                myCommand.ExecuteNonQuery();
                BindGrid();
            }
            conn.Close();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            Response.Redirect("CourseInsert.aspx");
        }

        protected void btnCno_Click(object sender, EventArgs e)
        {
            string Cno = txtQuery.Text.Trim().Replace("'", "''");
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            string strSql = string.Format("SELECT * FROM ctview WHERE cno LIKE '%{0}%'", Cno);
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataAdapter da = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "ctview");
            GridView1.DataSource = ds.Tables["ctview"];
            GridView1.DataBind();
        }

        protected void btnCname_Click(object sender, EventArgs e)
        {
            string Cname = txtQuery.Text.Trim().Replace("'", "''");
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            string strSql = string.Format("SELECT * FROM ctview WHERE cname LIKE '%{0}%'", Cname);
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataAdapter da = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "ctview");
            GridView1.DataSource = ds.Tables["ctview"];
            GridView1.DataBind();
        }

        protected void btnTname_Click(object sender, EventArgs e)
        {
            string Tname = txtQuery.Text.Trim().Replace("'", "''");
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            string strSql = string.Format("SELECT * FROM ctview WHERE tname LIKE '%{0}%'", Tname);
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataAdapter da = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "ctview");
            GridView1.DataSource = ds.Tables["ctview"];
            GridView1.DataBind();
        }
    }
}